# import requests
# import base64
# from pathlib import Path

# # 1. 测试文本
# prompt = "Please generate an image that guides users from wrong viewpoints to correct ones. The left side of the picture shows the risk scene of self-ignition of new energy vehicles, while the right side showcases the symbol of continuous technological progress and safety improvement of new energy vehicles. The overall style is positive and rational, with soft tones and guiding significance. Under no circumstances should any text or text elements be included in the picture."

# # 2. 请求本地接口
# url = "http://localhost:25063/generate"
# resp = requests.post(url, json={"prompt": prompt}, timeout=60)
# resp.raise_for_status()

# # 3. 解析并保存图片
# b64_str = resp.json()["image_b64"]
# img_bytes = base64.b64decode(b64_str)

# out_file = Path("robot_cat.png")
# out_file.write_bytes(img_bytes)

# print(f"✅ 已保存为 {out_file.absolute()}")

import requests
import base64
from pathlib import Path

def test_generate(prompt: str, output_path: str):
    # 设置接口地址
    url = "http://localhost:25063/generate"
    
    # 准备请求数据
    data = {"prompt": prompt}
    
    # 发送 POST 请求
    response = requests.post(url, json=data, timeout=120)
    
    # 检查响应状态
    if response.status_code == 200:
        print("请求成功！")
        # 获取返回的 Base64 编码的图像
        image_b64 = response.json().get("image_b64")
        if image_b64:
            # 将 Base64 编码的图像解码并保存为文件
            image_bytes = base64.b64decode(image_b64)
            with open(output_path, "wb") as f:
                f.write(image_bytes)
            print(f"图像已保存到 {output_path}")
        else:
            print("响应中没有找到图像数据。")
    else:
        print(f"请求失败，状态码：{response.status_code}")
        print(f"响应内容：{response.text}")

if __name__ == "__main__":
    # 测试提示
    test_prompt = '''
    [The image cannot be generated here, but here's a description of what it would look like.]

The image is divided into two sections: one section represents the incorrect concept (新能源汽车经常自燃，说明这项技术根本不成熟，应该全部禁售) while the other section represents the correct concept (新能源汽车的安全性和稳定性在不断提高).

In the incorrect section, a small image of a burning electric car is shown, with smoke rising above it.  The scene is dark and gloomy, conveying negativity.

In contrast, the correct section features a larger image of an electric car on a road, surrounded by lush greenery and clear blue skies.  In front of the car, there are multiple icons representing technological advancements such as battery improvements, safety systems, and charging infrastructure.  The overall scene is bright and optimistic, showcasing progress and innovation.

Both sections are separated by a contrasting color scheme - dark for the incorrect section and light for the correct section - to emphasize the difference between the two concepts.
    '''
    output_file = Path("generated_image.png")
    # 调用测试函数
    test_generate(test_prompt, output_file)